package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.Employee;
import org.example.erp.mapper.EmployeeMapper;
import org.example.erp.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    EmployeeMapper employeeMapper;

    @GetMapping("/talent/find")
    public DataResult find(
            @RequestParam(value = "employeeId") String employeeId,
            @RequestParam(value = "employeeName") String employeeName,
            @RequestParam(value = "education") String education,
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<Employee> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper();
        if (employeeId != null && !"".equals(employeeId)) {
            queryWrapper.lambda().like(Employee::getEmployeeCode, employeeId);
        }
        if (employeeName != null && !"".equals(employeeName)) {
            queryWrapper.lambda().like(Employee::getEmployeeName, employeeName);
        }
        if (education != null && !"".equals(education)) {
            queryWrapper.lambda().like(Employee::getEducation, education);
        }
        page = employeeService.getEmployeeAndRanks(page, queryWrapper);
        return DataResult.success(page);
    }

    @GetMapping("/talent/findCode")
    public DataResult findCode(@RequestParam(value = "employeeId") String employeeId) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Employee::getEmployeeCode, employeeId);
        Employee employee = employeeService.getOne(queryWrapper);
        return DataResult.success(employee);
    }

    @PostMapping("/talent/insert")
    public DataResult install(@RequestBody Employee employee) {
        boolean result = employeeService.addEmployee(employee);
        return DataResult.success(result ? "成功" : "失败");
    }

    @DeleteMapping("/talent/delete")
    public DataResult delete(@RequestBody List<String> id) {
        boolean result = employeeService.removeByIds(id);
        return DataResult.success(result ? "成功" : "失败");
    }

    @PutMapping("/talent/update")
    public DataResult update(@RequestBody Employee employee) {
        boolean result = employeeService.updateById(employee);
        return DataResult.success(result ? "成功" : "失败");
    }
}
